package StudyCode.summary.csv;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class QuotesCSVValue {
	public static final String QUOTE = "\"";

	public static void main(String[] args) {
		System.out.println(quoteCSVValue("www.tah1986.com", "."));
	}

	public static String quoteCSVValue(String value, String sep) {
		if (value == null) {
			return null;
		} else if (value.length() == 0) {
			return QUOTE + QUOTE;
		}

		value = value.replaceAll(QUOTE, QUOTE + QUOTE);

		boolean needQuote = false;
		needQuote = (value.indexOf(sep) != -1)
				|| (value.indexOf(QUOTE) != -1)
				|| (value.indexOf('\n') != -1)
				|| value.startsWith(" ") || value.endsWith(" ")
				|| value.startsWith("\t") || value.endsWith("\t");
		if (needQuote) {
			value = QUOTE + value + QUOTE;
		}

		return value;
	}
}
